#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;

namespace AbstractClass
{
    /// <summary>
    /// Provides some utility methods for <see cref="Enum"/>.
    /// </summary>
    public static class EnumUtil
    {
        /// <summary>
        /// Parses the specified <paramref name="valueString"/> as an enumeration specified by <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">Type of enumeration.</typeparam>
        /// <param name="valueString">The value string.</param>
        /// <returns>The enumeration value parsed from the specified string.</returns>
        public static T Parse<T>(string valueString) where T : struct
        {
            return Parse<T>(valueString, false);
        }

        /// <summary>
        /// Parses the specified <paramref name="valueString"/> as an enumeration specified by <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">Type of enumeration.</typeparam>
        /// <param name="valueString">The value string.</param>
        /// <param name="ignoreCase">if set to <c>true</c> ignore case.</param>
        /// <returns>The enumeration value parsed from the specified string.</returns>
        public static T Parse<T>(string valueString, bool ignoreCase) where T : struct
        {
            return typeof (T).IsEnum ? (T) Enum.Parse(typeof (T), valueString, ignoreCase) : default(T);
        }

        /// <summary>
        /// Tries to parse the specified <paramref name="valueString"/> as an enumeration specified by <typeparamref name="T"/>.
        /// If the parsing is successful <paramref name="value"/> will have the enumeration value, otherwise it will contain the
        /// default value of the enumeration.
        /// </summary>
        /// <typeparam name="T">Type of enumeration.</typeparam>
        /// <param name="valueString">The value string.</param>
        /// <param name="ignoreCase">if set to <c>true</c> it will ignore case.</param>
        /// <param name="value">The enumeration value obtained from parsing.</param>
        /// <returns><c>true</c> if parsing is successful; otherwise, <c>false</c>.</returns>
        public static bool TryParse<T>(string valueString, bool ignoreCase, out T value) where T : struct
        {
            if (typeof (T).IsEnum)
            {
                try
                {
                    value = Parse<T>(valueString, ignoreCase);
                    return true;
                }
                catch
                {
                    value = default(T);
                    return false;
                }
            }
            value = default(T);
            return false;
        }
    }
}